package event

import (
	"github.com/gdamore/tcell/v2"
	"github.com/mattn/go-runewidth"
	"go-TcellTxt/global"
	"go-TcellTxt/utils"
)

type MouseLeftListen struct {
	//输入的内容
	data  rune
	event tcell.Event
}

// 创建一个输入事件
func NewMouseLeftListen(data rune, event tcell.Event) *MouseLeftListen {
	return &MouseLeftListen{
		data:  data,
		event: event,
	}

}

func (i *MouseLeftListen) Handler() {

	for x1 := global.MouseX; x1 > 0; x1-- {
		//获取鼠标坐标的值
		r := utils.ScreenContent[global.MouseY][x1]
		//如果不是0
		if r != 0 {
			width := runewidth.RuneWidth(r)
			//判断几个字符就移动几个字符
			global.MouseX = global.MouseX - width
			utils.MoveCursor(global.Screen, global.MouseX, global.MouseY)
			return
		}
	}
	//如果到了最左边,那么移动上上一行的最后一个字符
	if global.MouseX == 0 && global.MouseY-1 >= 0 {
		global.MouseY--
		//计算一行的宽度
		width := runewidth.StringWidth(string(utils.ScreenContent[global.MouseY]))
		global.MouseX = width
		utils.MoveCursor(global.Screen, global.MouseX, global.MouseY)
	}
}
